/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package milkyway.hightech.testit;

import java.io.IOException;
import milkyway.hightech.entry.AccountStatement;
import java.util.Calendar;
import java.util.Observable;
import java.util.Observer;
import milkyway.hightech.banking.BankImpl;
import milkyway.hightech.comparators.SortOrder;
import milkyway.hightech.conditions.ConditionTypes;
import milkyway.hightech.customer.BankCustomerView;
import milkyway.hightech.logging.ProxyLogging;
import milkyway.hightech.money.Currency;
import milkyway.hightech.timer.TimeEmitter;

/**
 * 
 * @author michael
 */
public class TestItImpl implements Observer
{

    /**
     * Asures that the accounts where created only once
     */
    private boolean accountsOpen = false;
    
    /**
     * the bankimplementation for testing
     */
    private BankCustomerView customer1;
    private BankCustomerView customer2;
    
    private int accountNumber1;
    private int accountNumber2;
    
    /**
     * a textarea to print on the account statement
     */
    private AccountStatement accountStatement = new AccountStatement();
    
    public TestItImpl(BankImpl bank1, BankImpl bank2) throws IOException
    {
	this.customer1 = (BankCustomerView) ProxyLogging.newInstance(bank1);
	this.customer2 = (BankCustomerView) ProxyLogging.newInstance(bank2);
    }
         
    /***************************************************************************
     * Public method, just for testing the bankimplementation
     **************************************************************************/
    @Override
    public void update(Observable o, Object obj)
    {
	if (!accountsOpen)
	    accountsOpen = openAccounts();

	if (TimeEmitter.getInstance().getCalendar().getTime().getDate() == Calendar.getInstance().getTime().getDate() + 1)
	{
	    transferMoney("Sparkasse", accountNumber2, accountNumber1, 10, Currency.EUR, "Zparen10");
	    transferMoney("Sparkasse", accountNumber2, accountNumber1, 20, Currency.EUR, "Aparen20");
	    transferMoney("Sparkasse", accountNumber2, accountNumber1, 30, Currency.EUR, "Mparen30");
	}

	if (TimeEmitter.getInstance().getCalendar().getTime().getDate() == Calendar.getInstance().getTime().getDate() + 5)
	    printAccountStatements();
    }

    /***************************************************************************
     * End of public testing method
     **************************************************************************/
    
    
    
    /***************************************************************************
     * Private methods for testing bankfeautures
     **************************************************************************/
    private boolean openAccounts()
    {
	accountNumber1 = customer1.openGiroAccount("Michael J\u00fcnger", ConditionTypes.GROUP1, Currency.EUR);
	accountNumber2 = customer2.openGiroAccount("Sanim Rashid", ConditionTypes.GROUP1, Currency.EUR);
	System.out.println("Your account: " + accountNumber1 + " was succesfully opened!");
	System.out.println("Your account: " + accountNumber2 + " was succesfully opened!");

	return true;
    }

    private void payInMoney(int accountNumber, double amount,
	    Currency currency, String reasonOfTransfer)
    {
	customer1.payInMoney(accountNumber, amount, currency, reasonOfTransfer);
    }

    private void drawMoney(int accountNumber, double amount)
    {
	customer1.drawMoney(accountNumber, amount);
    }

    private void transferMoney(int sourceAccount, int targetAccount,
	    double amount, Currency currency, String reasonOfTransfer)
    {
	customer1.transferMoney(sourceAccount, targetAccount,
		amount, currency, reasonOfTransfer);
    }
    
    private void transferMoney(String bank, int sourceAccount, int targetAccount,
	    double amount, Currency currency, String reasonOfTransfer)
    {
	customer2.tranferMoney(bank, sourceAccount, targetAccount,
		amount, currency, reasonOfTransfer);
    }

    private void printAccountStatements()
    {
	accountStatement.writeTXT(customer1.getAccountStatement(accountNumber1, SortOrder.Amount));
	accountStatement.writeTXT("\n\n");
	accountStatement.writeTXT("-------------------------------------------------------------------------------------------------------------------------");
	accountStatement.writeTXT("\n\n");
	accountStatement.writeTXT(customer2.getAccountStatement(accountNumber2, SortOrder.Name));
	accountStatement.writeTXT("\n\n");
	accountStatement.writeTXT("==========================================================================================================================");
	accountStatement.writeTXT("\n\n");
    }
}
